/* ----------------------------------------------------------------------------
* File: utils.cpp
*
* Desc: Common utils for plugin
*
* Created: 20/12/2010, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#include "main.h"

using namespace std;


/************************************************************************/
/* Writes error description to the log file                             */
/************************************************************************/
void LogMessage(CHAR *lpszMsg, fstream &Log)
{
	if (Log.is_open())
	{
		Log << lpszMsg << endl;
		Log.flush();
	}
}


/************************************************************************/
/* Writes function error description to the log file                    */
/************************************************************************/
void LogErrorFunction(CHAR *lpszFunction, fstream &Log)
{
	CHAR szBuf[128];
	LPVOID lpMsgBuf;
	DWORD dw = GetLastError();

	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		dw,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpMsgBuf,
		0, NULL);

	sprintf(szBuf, "%s failed with error %d: %s", lpszFunction, dw, lpMsgBuf);

	if (Log.is_open())
	{
		Log << szBuf << endl;
		Log.flush();
	}

	return;
}


/************************************************************************/
/* Output debug information                                             */
/************************************************************************/
void MyOutputDebugString(const TCHAR *str, ...)
{
	TCHAR buf[2048];
	va_list ptr;

	va_start(ptr,str);

	_vstprintf(buf, str, ptr);
	OutputDebugString(buf);

	va_end(ptr);
}